﻿// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Data;
using System.Data.Common;
using SnapObjects.Commons;
using JetBrains.Annotations;
using Oracle.ManagedDataAccess.Client;

namespace SnapObjects.Data.Oracle
{
    public class OracleClobTypeMapping : OracleTypeMapping
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="OracleStringTypeMapping" /> class.
        /// </summary>
        /// <param name="dbTypeName"> The name of the database type. </param>
        /// <param name="dbType"> The <see cref="DbType" /> to be used. </param>
        /// <param name="unicode"> A value indicating whether the type should handle Unicode data or not. </param>
        /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
        public OracleClobTypeMapping(
            [NotNull] string dbTypeName,
            [NotNull] OracleDbType oracleDbType,
            [CanBeNull] DbType? dbType,
            bool unicode = false,
            int? size = null)
            : base(dbTypeName, typeof(string), dbType, unicode, size)
        {
            this.OracleDbType = oracleDbType;
        }

        private OracleDbType OracleDbType { get; set; }

        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override TypeMapping Clone(string dbTypeName, int? size)
        {
            return new OracleClobTypeMapping(
                dbTypeName, this.OracleDbType, this.DbType, this.IsUnicode, size);
        }

        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected override void ConfigureParameter(DbParameter parameter, IAdoDbParameter adoDbParameter)
        {
            if (parameter is OracleParameter oracleParameter)
            {
                oracleParameter.OracleDbType = this.OracleDbType;

                if ("long".Equals(this.DbTypeName) && 
                    parameter.Size < 1 &&
                    (oracleParameter.Direction == ParameterDirection.Output || 
                    oracleParameter.Direction == ParameterDirection.InputOutput ||
                    oracleParameter.Direction == ParameterDirection.ReturnValue))
                {
                    parameter.Size = 32767;
                }
            }
        }

        /// <summary>
        ///     Generates the SQL representation of a literal value.
        /// </summary>
        /// <param name="value">The literal value.</param>
        /// <returns>
        ///     The generated string.
        /// </returns>
        protected override string GenerateNonNullSqlLiteral(object value)
        {
            return this.IsUnicode
                           ? $"N'{this.EscapeSqlLiteral((string)value)}'" // Interpolation okay; strings
                           : $"'{this.EscapeSqlLiteral((string)value)}'";
        }

        /// <summary>
        ///     Generates the escaped SQL representation of a literal value.
        /// </summary>
        /// <param name="literal">The value to be escaped.</param>
        /// <returns>
        ///     The generated string.
        /// </returns>
        private string EscapeSqlLiteral([NotNull] string literal)
        {
            return Check.NotNull(literal, nameof(literal)).Replace("'", "''");
        }

        protected override bool IsAssignable(Type modelType, Type dbType)
        {
            return base.IsAssignable(modelType, dbType) ||
                modelType == typeof(char);
        }
    }
}
